<?php
class Manager_CoreClass {
	private $obj_load = array();
	private $page = 'name';
	private $layout = 'layout_template';
	private $error = array();
	private static $allow_obj = array('HTML'=>'htmlClass','DB'=>'managerDBClass');
	
	
	public function setLoad ($arr)
	{
		if (is_array($arr))
		{
			$this->obj_load = $arr;
		}
	}
	
	public static function listControllerView ()
	{
		$list = array();
		$dir = opendir(APP_PATH.DS.'controllers_views');
		while ($file = readdir($dir)) {
	
		  	// if file isn't this directory or its parent, add it to the results
		  	if ($file != "." && $file != ".." && strpos(strtolower($file), 'view')!==false) {
		    	$list[]=(substr($file, 0,-8));
		  	}
		}
		return $list;
	}
		
	public function run($args,$routing)
	{		 
		$variable_load = array();
		$html_content='';
		
		if (DEBUG)
		{
			if (!file_exists(APP_PATH.DS.'includes'.DS.'elements'.DS.$this->layout.'.php'))
				fb($this->layout.'.php inesistente','Template',FirePHP::ERROR);
		}
		
		// load class 
		if (!empty($this->obj_load))
		{
			foreach ($this->obj_load as $class)
			{
				$variable_load[$class]= new self::$allow_obj[$class];
			}
			extract($variable_load);
			unset($variable_load);		
		}
		if (DEBUG) fb(implode(',',$args),'Lista Argomenti GET',FirePHP::INFO);
	
		// controllo routing impostati
		if (!empty($routing))
		{
			
			if (!empty($args) && isset($routing[$args[0]]))
			{
				$this->page=$routing[$args[0]]['redir_page'];
				if (isset($routing[$args[0]]['template']) && $routing[$args[0]]['template']!='')
					$this->layout = $routing[$args[0]]['template'];
			} 
			else if (isset($routing['*']))
			{
				$this->page=$routing['*']['redir_page'];
				if (isset($routing['*']['template']) && $routing['*']['template']!='')
					$this->layout = $routing['*']['template'];
			}	
		}else
			$this->page=!empty($args)?$args[0]:$this->page;
		
		
		// carico controller
		try
		{
			if (!file_exists(APP_PATH.DS.'controllers_views'.DS.$this->page.'Controller.php'))
					$this->page='home';
			if (DEBUG>1) fb($this->page.'Controller.php','Controller',FirePHP::INFO);
				require_once (APP_PATH.DS.'controllers_views'.DS.$this->page.'Controller.php');
		}catch (exception $ex) { $this->error[]=$ex->getMessage(); }
		 
		
			
		// carico viewer
		try
		{
			ob_start();	
			if (!file_exists(APP_PATH.DS.'controllers_views'.DS.$this->page.'View.php'))
				$this->page='home';
			if (DEBUG>1) fb($this->page.'View.php','Viewer',FirePHP::INFO);	
			require_once (APP_PATH.DS.'controllers_views'.DS.$this->page.'View.php');
			$html_content = ob_get_contents();
			ob_end_clean();
		} catch (exception $ex) { $this->error[]=$ex->getMessage(); }
			
		// carico layout
		try		
		{	
			if (DEBUG>1) fb($this->layout.'.php','Template',FirePHP::INFO);
			if (!file_exists(APP_PATH.DS.'includes'.DS.'elements'.DS.$this->layout.'.php'))
				$this->layout='layout_template';
			require_once (APP_PATH.DS.'includes'.DS.'elements'.DS.$this->layout.'.php');
			
		} catch (exception $ex) { $this->error[]=$ex->getMessage(); }
	}

}

?>